package com.mq.mqtest.controller;

import com.mq.mqtest.entity.Order;
import com.mq.mqtest.entity.User;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;

/**
 * @author peng.li
 * @date 2021/6/14 19:31
 */
@RestController
@RequestMapping("test")
public class TestController {
    private String exchangeName = "boot-exchange1";
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*
   测试发送两种不同内容类型的消息
    */
    @GetMapping("createOrder")
    public String createOrder() {
        //下单成功
     Order order = new Order();
     order.setId(Long.valueOf(new Random().nextInt(100000) + 122231));
     order.setUserAddress("香港");
     order.setCreateTime(LocalDateTime.now().toString());
     //给MQ发送消息
        //String exchange, String routingKey, Object object
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",order);
        System.out.println("订单id：" + order.getId());
        return "create success，create time:" + order.getCreateTime();
    }


    /*
    测试发送两种不同内容类型的消息
     */
    @GetMapping("send")
    public String test(@RequestParam(value = "index", defaultValue = "10") Integer index) {
        for (int x = 0; x < index; x++){
                User user = new User();
                user.setId(1L);
                user.setName("张希悦");
                user.setSex("女");
                rabbitTemplate.convertAndSend(exchangeName, "exchange-queue", user, new CorrelationData(UUID.randomUUID().toString()));
        }
        return "success";
    }
}
